package future;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * LockSupport相关测试类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/08/06 下午 01:44
 */
public class LockSupportTest {
    final Object objLock = new Object();

    @Test
    public void testWaitAndNotify() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (objLock) {
                System.out.println("t1进入----");
                try {
                    System.out.println("t1放弃锁并等待----");
                    objLock.wait();
                    System.out.println("t1被唤醒----");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("t1退出---");
            }
        }, "t1");
        t1.start();
        Thread.sleep(1000);
        Thread t2 = new Thread(() -> {
            synchronized (objLock) {
                System.out.println("t2进入---");
                System.out.println("t2唤醒等待锁的线程---");
                objLock.notify();
                System.out.println("t2退出");
            }
        }, "t2");
        t2.start();

        t1.join();
        t2.join();
    }

    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    @Test
    public void testAwaitAndSignal() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("t1进入");
                try {
                    condition.await();
                    System.out.println("t1唤醒");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                lock.unlock();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("t2进入");
                condition.signal();
                System.out.println("t2唤醒t1");
            } finally {
                lock.unlock();
            }
        }, "t2");

        t1.start();
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();

        t1.join();
        t2.join();
    }


    @Test
    public void parkAndUnParkTest() throws InterruptedException {
        Thread t1 = new Thread(() -> {
             try { TimeUnit.MILLISECONDS.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println("t1进入");
            LockSupport.park();
            LockSupport.park();
            System.out.println("t1被唤醒");
        }, "t1");

        Thread t2 = new Thread(() -> {
            System.out.println("t2进入");
            System.out.println("t2唤醒t1");
            LockSupport.unpark(t1);
            LockSupport.unpark(t1);
        }, "t2");

        
        t1.start();
        Thread.sleep(1000);
        t2.start();

        t1.join();
        t2.join();
    }


}
